// Copyright (C) Kumo inc. and its affiliates.
// Author: Jeff.li lijippy@163.com
// All rights reserved.
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU Affero General Public License as published
// by the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU Affero General Public License for more details.
//
// You should have received a copy of the GNU Affero General Public License
// along with this program.  If not, see <https://www.gnu.org/licenses/>.
//

#include <gtest/gtest.h>

#include <cantor/function/builtin.h>
#include <cantor/function/resolver.h>

class BuiltinTest : public ::testing::Test {
public:
    virtual void SetUp() {
        registry = std::make_unique<cantor::FunctionRegistry>();
        success = cantor::initialize_builtin(registry.get());
    }

    virtual void TearDown() {
        registry.reset();
    }

protected:
    turbo::Status success;
    std::unique_ptr<cantor::FunctionRegistry> registry;
};

TEST_F(BuiltinTest, base) {
    auto set = registry->find_function_set("add");
    ASSERT_NE(nullptr, set);

    auto set_sub = registry->find_function_set("subtract");
    ASSERT_EQ(nullptr, set_sub);
    {
        cantor::pb::catalog::FunctionResolveArgs args;
        args.set_func_name("add");
        auto ptr = args.mutable_args()->Add();
        ptr->set_type(cantor::pb::TYPE_KIND_INT8);

        ptr = args.mutable_args()->Add();
        ptr->set_type(cantor::pb::TYPE_KIND_INT8);

        cantor::pb::catalog::FunctionResolveResult result;
        resolve_function(registry.get(), args, result);
        std::cout<<result.ShortDebugString()<<std::endl;
    }
    {
        cantor::pb::catalog::FunctionResolveArgs args;
        args.set_func_name("add");
        auto ptr = args.mutable_args()->Add();
        ptr->set_type(cantor::pb::TYPE_KIND_INT8);

        ptr = args.mutable_args()->Add();
        ptr->set_type(cantor::pb::TYPE_KIND_INT32);

        cantor::pb::catalog::FunctionResolveResult result;
        resolve_function(registry.get(), args, result);
        std::cout<<result.ShortDebugString()<<std::endl;
    }
    {
        cantor::pb::catalog::FunctionResolveArgs args;
        args.set_func_name("add");
        auto ptr = args.mutable_args()->Add();
        ptr->set_type(cantor::pb::TYPE_KIND_INT32);

        ptr = args.mutable_args()->Add();
        ptr->set_type(cantor::pb::TYPE_KIND_INT8);

        cantor::pb::catalog::FunctionResolveResult result;
        resolve_function(registry.get(), args, result);
        std::cout<<result.ShortDebugString()<<std::endl;
    }
    {
        cantor::pb::catalog::FunctionResolveArgs args;
        args.set_func_name("add");
        auto ptr = args.mutable_args()->Add();
        ptr->set_type(cantor::pb::TYPE_KIND_INT32);

        ptr = args.mutable_args()->Add();
        ptr->set_type(cantor::pb::TYPE_KIND_INT32);

        cantor::pb::catalog::FunctionResolveResult result;
        resolve_function(registry.get(), args, result);
        std::cout<<result.ShortDebugString()<<std::endl;
    }
}